using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Network;
using MicroMsg.Scene;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
namespace MicroMsg.Plugin.Sns.Scene
{
	public class NetSceneSnsSync : NetSceneBaseEx<SnsSyncRequest, SnsSyncResponse, SnsSyncRequest.Builder>
	{
		private const string TAG = "NetSceneSnsSync";
		private const int MAX_CONTINUE_SYNC_COUNT = 10;
		private EventWatcher mWatcher;
		private int mContiuneSyncCount;
		private int mSyncStatus;
		public bool isEnable;
		private bool hasNotify;
		private static NetSceneSnsSync _mInstance;
		public static NetSceneSnsSync Instance
		{
			get
			{
				if (NetSceneSnsSync._mInstance == null)
				{
					NetSceneSnsSync._mInstance = new NetSceneSnsSync();
				}
				return NetSceneSnsSync._mInstance;
			}
		}
		public static bool enable
		{
			set
			{
				NetSceneSnsSync.Instance.isEnable = value;
				if (value)
				{
					if (NetSceneSnsSync.Instance.hasNotify)
					{
						NetSceneSnsSync.Instance.doScene();
						return;
					}
				}
				else
				{
					if (NetSceneSnsSync.Instance.isSyncNow())
					{
						NetSceneSnsSync.Instance.cancel();
						NetSceneSnsSync.Instance.hasNotify = true;
					}
				}
			}
		}
		public static void Init()
		{
			if (NetSceneSnsSync._mInstance == null)
			{
				NetSceneSnsSync._mInstance = new NetSceneSnsSync();
				NetSceneSnsSync._mInstance.isEnable = true;
			}
		}
		public NetSceneSnsSync()
		{
			this.mWatcher = new EventWatcher(this, null, new EventHandlerDelegate(NetSceneSnsSync.HandlerDoScene));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_NOTIFY_SYNC_REQ, this.mWatcher);
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_SYNC, this.mWatcher);
		}
		public bool isSyncNow()
		{
			return this.mSyncStatus == 1;
		}
		public bool doScene()
		{
			if (!this.isEnable)
			{
				Log.smethod_1("NetSceneSnsSync", "sns isEnable is false");
				return false;
			}
			if (this.mSyncStatus == 1)
			{
				Log.smethod_4("NetSceneSnsSync", "is doing snsSync now,return this request");
				return true;
			}
			this.mSyncStatus = 1;
			this.hasNotify = false;
			this.continueDoScene();
			return true;
		}
		private bool continueDoScene()
		{
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			this.mBuilder.Selector = 256u;
			this.mBuilder.KeyBuf = Util.toSKBuffer(NetSceneNewSync.mBytesSyncKeyBuf);
			this.mSessionPack.mCmdID = 102;
			base.endBuilder();
			if (this.mContiuneSyncCount < 10)
			{
				this.mContiuneSyncCount++;
			}
			else
			{
				Log.smethod_5("NetSceneSnsSync", "mSyncCount exceed the maximum");
			}
			return true;
		}
		protected override void onSuccess(SnsSyncRequest request, SnsSyncResponse response)
		{
			int ret = response.BaseResponse.Ret;
			if (ret != 0)
			{
				Log.smethod_1("NetSceneSnsSync", "doScene: snsSync failed!" + ret);
				this.mSyncStatus = 2;
				return;
			}
			Log.smethod_4("NetSceneSnsSync", string.Concat(new object[]
			{
				"doScene: snsSync success, continueFlag = ",
				response.ContinueFlag,
				" response.CmdList.Count = ",
				response.CmdList.Count
			}));
			List<object> cmdObjectList = NetSceneNewSync.getCmdObjectList(response.CmdList.ListList);
			this.ProcessCmdItem(request, response, cmdObjectList);
		}
		private bool ProcessCmdItem(SnsSyncRequest request, SnsSyncResponse response, List<object> cmdObjectList)
		{
			NetSceneSnsSync.RespHandler(cmdObjectList);
			if (!this.saveInfo(request, response))
			{
				Log.smethod_1("NetSceneSnsSync", "SaveUserInfo failed");
			}
			if (this.canContinue(response))
			{
				Log.smethod_4("NetSceneSnsSync", "doScene: snsSync NewSyncContinue");
				this.continueDoScene();
				return true;
			}
			Log.smethod_4("NetSceneSnsSync", "doScene: snsSync finished");
			this.mSyncStatus = 0;
			this.resetFlag();
			return true;
		}
		private void resetFlag()
		{
			this.mContiuneSyncCount = 0;
		}
		protected override void onFailed(SnsSyncRequest request, SnsSyncResponse response)
		{
			PackResult lastError = base.getLastError();
			this.resetFlag();
			Log.smethod_1("NetSceneSnsSync", "doScene: snsSync failed! packResult =" + lastError);
			this.mSyncStatus = 2;
		}
		private bool canContinue(SnsSyncResponse resp)
		{
			return (resp.ContinueFlag & 256u) != 0u && this.mContiuneSyncCount < 10;
		}
		private bool saveInfo(SnsSyncRequest request, SnsSyncResponse response)
		{
			if (request != null && response != null)
			{
				NetSceneNewSync.mBytesSyncKeyBuf = NetSceneNewInit.mergeKeyBuf(request.KeyBuf.Buffer.ToByteArray(), response.KeyBuf.Buffer.ToByteArray());
				SyncInfo info = new SyncInfo(NetSceneNewSync.mBytesSyncKeyBuf);
				AccountMgr.saveSyncInfoAsync(info);
				return true;
			}
			Log.smethod_1("NetSceneSnsSync", "invalid snsSyncReq or snsSyncResp");
			return false;
		}
		private static void HandlerDoScene(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			Log.smethod_4("NetSceneSnsSync", "onHander NetSceneSnsSync mEventID = " + evtArgs.mEventID);
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_SYNC)
			{
				NetSceneSyncResult netSceneSyncResult = evtArgs.mObject as NetSceneSyncResult;
				if (netSceneSyncResult == null)
				{
					Log.smethod_1("NetSceneSnsSync", "NetSceneSyncResult == null");
					return;
				}
				if (netSceneSyncResult.syncStatus == SyncStatus.syncEnd && netSceneSyncResult.syncCount == 0)
				{
					Log.smethod_5("NetSceneSnsSync", "onHander sync event result.syncStatus = " + netSceneSyncResult.syncStatus);
					NetSceneSnsSync.Instance.doScene();
					return;
				}
				if (netSceneSyncResult.syncStatus == SyncStatus.syncEnd && netSceneSyncResult.isAppActive)
				{
					Log.smethod_5("NetSceneSnsSync", "onHander sync event result.syncStatus = " + netSceneSyncResult.syncStatus);
					NetSceneSnsSync.Instance.doScene();
				}
				return;
			}
			else
			{
				int num = 0;
				if (evtArgs.mObject != null)
				{
					num = (int)evtArgs.mObject;
				}
				if ((num & 256) == 0)
				{
					return;
				}
				NetSceneSnsSync.Instance.hasNotify = true;
				NetSceneSnsSync.Instance.doScene();
				return;
			}
		}
		public static void RespHandler(List<object> cmdObjectList)
		{
			Log.smethod_4("NetSceneSnsSync", "parsed cmdlist count =" + cmdObjectList.get_Count());
			for (int i = 0; i < cmdObjectList.get_Count(); i++)
			{
				object obj = cmdObjectList.get_Item(i);
				if (CmdItemHelper.mapCmdId.ContainsKey(obj.GetType()))
				{
					SyncCmdID syncCmdID = CmdItemHelper.mapCmdId.get_Item(obj.GetType());
					switch (syncCmdID)
					{
					case SyncCmdID.MM_SNS_SYNCCMD_OBJECT:
						NetSceneSnsSync.processSnsObject(obj as SnsObject);
						break;
					case SyncCmdID.MM_SNS_SYNCCMD_ACTION:
						NetSceneSnsSync.processSnsActionGroup(obj as SnsActionGroup);
						break;
					default:
						Log.smethod_5("NetSceneSnsSync", "doCmd: no processing method, cmd id=" + syncCmdID);
						break;
					}
				}
			}
		}
		private static void processSnsObject(SnsObject cmdSnsObj)
		{
			if (cmdSnsObj == null)
			{
				Log.smethod_1("NetSceneSnsSync", "processSnsObject,invalid object");
				return;
			}
			if (StorageMgr.snsInfo.getByObjectID(SnsInfo.toStrID(cmdSnsObj.Id)) == null)
			{
				EventCenter.postEvent(EventConst.ON_NETSCENE_SNS_SYNC_NEW_OBJ, SnsInfoMgr.toSnsInfo(cmdSnsObj), null);
			}
			SnsInfoMgr.update(cmdSnsObj);
		}
		private static void processSnsActionGroup(SnsActionGroup cmdSnsActionGp)
		{
			if (cmdSnsActionGp == null)
			{
				Log.smethod_1("NetSceneSnsSync", "processSnsActionGroup,invalid object");
				return;
			}
			if (SnsAsyncMgr.isDeletedObjectID(cmdSnsActionGp.Id))
			{
				return;
			}
			SnsMsg snsMsg = new SnsMsg();
			snsMsg.strObjectID = SnsInfo.toStrID(cmdSnsActionGp.Id);
			snsMsg.strParentID = SnsInfo.toStrID(cmdSnsActionGp.ParentId);
			snsMsg.strFromUsername = cmdSnsActionGp.CurrentAction.FromUsername;
			snsMsg.strToUsername = cmdSnsActionGp.CurrentAction.ToUsername;
			snsMsg.strFromNickname = cmdSnsActionGp.CurrentAction.FromNickname;
			snsMsg.strToNickname = cmdSnsActionGp.CurrentAction.ToNickname;
			snsMsg.nType = cmdSnsActionGp.CurrentAction.Type;
			snsMsg.nSource = cmdSnsActionGp.CurrentAction.Source;
			snsMsg.nCreateTime = cmdSnsActionGp.CurrentAction.CreateTime;
			snsMsg.strContent = cmdSnsActionGp.CurrentAction.Content;
			snsMsg.nCommentId = cmdSnsActionGp.CurrentAction.CommentId;
			snsMsg.nReplyCommentId = cmdSnsActionGp.CurrentAction.ReplyCommentId;
			if (cmdSnsActionGp.ReferAction.CreateTime != 0u)
			{
				snsMsg.refer = new SnsMsgDetail
				{
					strFromUsername = cmdSnsActionGp.ReferAction.FromUsername,
					strToUsername = cmdSnsActionGp.ReferAction.ToUsername,
					strFromNickname = cmdSnsActionGp.ReferAction.FromNickname,
					strToNickname = cmdSnsActionGp.ReferAction.ToNickname,
					nType = cmdSnsActionGp.ReferAction.Type,
					nSource = cmdSnsActionGp.ReferAction.Source,
					nCreateTime = cmdSnsActionGp.ReferAction.CreateTime,
					strContent = cmdSnsActionGp.ReferAction.Content,
					nCommentId = cmdSnsActionGp.ReferAction.CommentId,
					nReplyCommentId = cmdSnsActionGp.ReferAction.ReplyCommentId
				};
			}
			StorageMgr.snsMsg.update(snsMsg);
			SnsInfoMgr.updateComment(cmdSnsActionGp);
		}
	}
}
